﻿namespace SGS.IO
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Xml;
    using Ionic.Zip;
    using System.Xml.Serialization;
    using SGS.Logics;
    using System.Text;
    using SGS.Logics.Data;
    using System.Drawing;

    public class IOManager
    {
        public static readonly string DataFolderName = "Data";
        public static readonly string ImageFolderName = "Data//Images";
        public static readonly string LogFolderName = "Data//Log";
        public static readonly string AuctionFolderName = "Data//Images//Auction";
        private static IOManager instance = null;

        ~IOManager()
        {
            if (_Writer != null)
                _Writer.WriteLine(DateTime.Now.ToString());
            if (_Stream != null)
                _Stream.Close();
        }

        public static IOManager Instance
        {
            get
            {
                if (IOManager.instance == null)
                {
                    IOManager.instance = new IOManager();
                }
                return IOManager.instance;
            }
        }

        public string DataFolderPath
        {
            get
            {
                string path = Directory.GetCurrentDirectory() + "\\" + DataFolderName;
                if (Directory.Exists(path) == false)
                    Directory.CreateDirectory(path);
                return path;
            }
        }

        public string ImagesFolderPath
        {
            get
            {
                string path = Directory.GetCurrentDirectory() + "\\" + ImageFolderName;
                if (Directory.Exists(path) == false)
                    Directory.CreateDirectory(path);
                return path;
            }
        }

        public string LogFolderPath
        {
            get
            {
                string path = Directory.GetCurrentDirectory() + "\\" + LogFolderName;
                if (Directory.Exists(path) == false)
                    Directory.CreateDirectory(path);
                return path;
            }
        }

        public string AuctionFolderPath
        {
            get
            {
                string path = Directory.GetCurrentDirectory() + "\\" + AuctionFolderName;
                if (Directory.Exists(path) == false)
                    Directory.CreateDirectory(path);
                return path;
            }
        }

        public string GenerateDataPathName()
        {
            for (int i = 0; ; i++)
            {
                string fileName = "AuctionCard" + i.ToString() + ".bmp";
                string path = AuctionFolderPath + "//" + fileName;
                if (File.Exists(path) == false)
                    return path;
            }
        }

        private FileStream _Stream = null;
        private StreamWriter _Writer = null;
        public void Log(string text)
        {
            if (_Writer == null)
            {
                _Stream = new FileStream(this.LogFolderPath + "//" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm") + "_log.txt", FileMode.OpenOrCreate);
                _Writer = new StreamWriter(_Stream);

                _Writer.WriteLine(DateTime.Now.ToString());
            }

            _Writer.WriteLine(DateTime.Now.ToString() + " " + text);
            _Writer.Flush();
        }

        public void CloseLogFile()
        {
            if (_Stream != null)
            {
                _Stream.Close();
                _Stream = null;
            }
            if (_Writer != null)
            {
                _Writer = null;
            }
        }

        public string SaveImageToFile(Bitmap bitmap)
        {
            string ret = this.ImagesFolderPath + "\\modi.bmp";
            bitmap.Save(ret);
            return ret;
        }
    }
}
